﻿@model ProductModel

@using Telerik.Web.Mvc.UI;

@if (Model.Id > 0)
{
    if (Model.NumberOfAvailableManufacturers > 0)
    {
        <script>
			function onProductManufacturerEdit(e) {
				if (e.mode == "edit") {
					$('#Manufacturer')
						.data('select-selected-id', e.dataItem['ManufacturerId'])
						.data('select-init-text', e.dataItem['Manufacturer'])
						.val(e.dataItem['ManufacturerId'])
						.trigger('change');
				}
			}

			function onProductManufacturerDataBound(e) {
				$('#Manufacturer').selectWrapper({ resetDataUrl: '@Url.Action("AllManufacturers", "Manufacturer")' });
			}
        </script>
	
		@(Html.Telerik().Grid<ProductModel.ProductManufacturerModel>()
			.Name("productmanufacturers-grid")
			.DataKeys(keys => { keys.Add(x => x.Id); })
			.DataBinding(dataBinding =>
			{
				dataBinding.Ajax()
					.Select("ProductManufacturerList", "Product", new { productId = Model.Id })
					.Insert("ProductManufacturerInsert", "Product", new { productId = Model.Id })
					.Update("ProductManufacturerUpdate", "Product")
					.Delete("ProductManufacturerDelete", "Product");
			})
			.Columns(columns =>
			{
				columns.Bound(x => x.Manufacturer)
					.Width("60%")
					.Template(x => Html.ActionLink(x.Manufacturer, "Edit", "Manufacturer", new { id = x.ManufacturerId }))
					.ClientTemplate("<a href=\"" + @Url.Content("~/Admin/Manufacturer/Edit/") + "<#= ManufacturerId #>\"><#= Manufacturer #></a>");
				columns.Bound(x => x.IsFeaturedProduct)
					.Width(100)
					.Template(item => Html.SymbolForBool(item.IsFeaturedProduct))
					.ClientTemplate(Html.SymbolForBool("IsFeaturedProduct"))
					.Centered();
				columns.Bound(x => x.DisplayOrder)
					.Centered()
					.Width(100);
				columns.Command(commands =>
				{
					commands.Edit().Localize(T);
					commands.Delete().Localize(T);
				})
				.HtmlAttributes(new { align = "right" });
			})
			.ToolBar(commands => commands.Insert())
			.ClientEvents(events =>
			{
				events.OnEdit("onProductManufacturerEdit");
				events.OnDataBound("onProductManufacturerDataBound");
			})
			.EnableCustomBinding(true))
    }
    else
    {
        <div class="alert alert-danger">
            @T("Admin.Catalog.Products.Manufacturers.NoManufacturersAvailable")
        </div>
    }
}
else
{
    <div class="alert alert-danger">
        @T("Admin.Catalog.Products.Manufacturers.SaveBeforeEdit")
    </div>
}